package com.sgcc.sx.pmos.common.utils;

import com.blankj.utilcode.util.TimeUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {

    public static String getStrng_to_Times_ymd_1(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_2(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_3(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_y(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_4(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("MM月");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_4_1(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("MM");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_5(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static String getStrng_to_Times_ymd_6(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result;
            result = df.parse(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }

    public static Date getStrng_to_Times_ymd_7(String time) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
            Date result = df.parse(time);
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    public static String getCurrentTimes() {
        try {
            Date result = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            return sdf.format(result);
        } catch (Exception e) {
            return "";
        }
    }


    /**
     * 根据当前月份 之前或者之后月份
     *
     * @param num -之前 +之后
     * @return
     */
    public static String getMonthBackString(int num, String pattern) {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例

        ca.setTime(new Date()); // 设置时间为当前时间

        ca.add(Calendar.MONTH, num);// 月份减1
        Date resultDate = ca.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(resultDate);
    }

    /**
     * 根据当前月份 之前或者之后时间 如果是12月返回下一年
     *
     * @param num -之前 +之后
     * @return
     */
    public static String getMonthBackString1(int num, String pattern) {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        String mm = TimeUtils.date2String(TimeUtils.getNowDate(), "MM");
        if (mm.equals("12")) {
            String yyyy = TimeUtils.date2String(TimeUtils.getNowDate(), "yyyy");
            int year = Integer.parseInt(yyyy);
            ca.set(year + 1, 0, 1); // 设置时间为当前时间
        } else {
            ca.setTime(new Date()); // 设置时间为当前时间
        }
        ca.add(Calendar.MONTH, num);// 月份减1
        Date resultDate = ca.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(resultDate);
    }

    /**
     * 根据当前月份 之前或者之后月份
     *
     * @param num -之前 +之后
     * @return
     */
    public static Date getMonthBackDate(int num) {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.MONTH, num);// 月份减1
        Date resultDate = ca.getTime(); // 结果
        return resultDate;
    }

    public static Date nextMonthDate(int nextMonth) {
        Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.MONTH, nextMonth);// 月份加2
        Date nowDate = ca.getTime();
        return nowDate;
    }

}
